//
//  YGLoginView.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/3/14.
//

#import "YGLoginView.h"
#import "JGCountDownButton.h"


@interface YGLoginView () <UITextViewDelegate>
@property (nonatomic, strong) UIButton *accountBtn;
@property (nonatomic, strong) UIButton *smsBtn;


@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) UITextField *phoneTextField;
@property (nonatomic, strong) UITextField *pwdTextField;    //也是密码框，也是验证码狂
@property (nonatomic, strong) UIView *pwdBottomLine;    //底部线条

@property (nonatomic, strong) JGCountDownButton *countdownBtn;    //验证码
@property (nonatomic, assign) LoginType loginType;

@property (nonatomic, assign) UIImageView  *imageasd;

@property (nonatomic, strong) UIButton  *loginBtn;

@end
@implementation YGLoginView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.loginType = LoginPWDType;
        [self initView];
        [self initContraints];
    }
    return self;
}

- (void)initView{
    
    [self addSubview:self.accountBtn];
    [self addSubview:self.smsBtn];
    [self addSubview:self.loginBtn];
    [self addSubview:self.lineView];
    [self addSubview:self.phoneTextField];
    [self addSubview:self.pwdTextField];
    [self addSubview:self.countdownBtn];

}

- (void)initContraints{
    [self.accountBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(14);
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.accountBtn.mas_right).offset(5);
        make.height.mas_equalTo(14);
        make.centerY.mas_equalTo(self.accountBtn.mas_centerY);
        make.width.mas_equalTo(0.5);
    }];
    
    [self.smsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.lineView.mas_right).offset(5);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(14);
    }];
        
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.accountBtn.mas_bottom).offset(25);
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(self.accountBtn.mas_left);
        make.right.mas_equalTo(self.mas_right);
    }];
    
    [self.pwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.phoneTextField.mas_bottom).offset(10);
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(self.accountBtn.mas_left);
        make.right.mas_equalTo(self.mas_right);
    }];
    
    [self.countdownBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.pwdTextField.mas_top);
        make.height.mas_equalTo(self.pwdTextField.mas_height);
        make.left.mas_equalTo(self.pwdTextField.mas_right).offset(20);
        make.width.mas_equalTo(108);
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.pwdTextField.mas_bottom).offset(30);
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.height.mas_equalTo(50);
    }];
}

- (void)clickAccountAction:(UIButton *)sender{
    sender.selected = YES;
    self.smsBtn.selected = NO;
    self.pwdTextField.placeholder = @"请输入密码";
    self.loginType = LoginPWDType;
    [self updateConstraint];
}

- (void)clickSmsAction:(UIButton *)sender{
    sender.selected = YES;
    self.accountBtn.selected = NO;
    self.pwdTextField.placeholder = @"请输入验证码";
    self.loginType = LoginSMSType;
    [self updateConstraint];
}

- (void)clickAuthBtnAction:(UIButton *)sender{
    if (self.phoneTextField.text.length != 11) {
        //非11位
//        [self showTip:@"请输入正确的手机号"];
        return ;
    }
    [self requestImageVcode];
}

- (void)clickLoginAction{
    if ([self.delegate respondsToSelector:@selector(loginViewLoginWithPhone:pwd:loginType:)]) {
        [self.delegate loginViewLoginWithPhone:self.phoneTextField.text pwd:self.pwdTextField.text loginType:self.loginType];
    }
}

- (void)updateConstraint{
    if (self.loginType == LoginPWDType) {
        [self.pwdTextField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right);
        }];
        self.countdownBtn.hidden = YES;
    }else{
        self.countdownBtn.hidden = NO;
        [self.pwdTextField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).offset(-133);
        }];
    }
    [self.pwdBottomLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.pwdTextField.mas_width);
    }];
}

- (void)requestImageVcode {
    
    //请求验证码网络，
}

- (void)setLoginButtonStatus:(BOOL)status {
    if (status) {
        self.loginBtn.userInteractionEnabled = YES;
        self.loginBtn.backgroundColor = UIColorFromRGB(0x37C8D7);
    } else {
        self.loginBtn.userInteractionEnabled = NO;
        self.loginBtn.backgroundColor = UIColorFromRGBAlpha(0x37C8D7, 0.5);
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == self.phoneTextField) {
        NSString *regex =@"[0-9]*";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        if ([pred evaluateWithObject:string]) {
            return YES;
        }
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//    [self onLoginBtnClicked:self.loginButton];
    return YES;
}

- (void)textFieldDidChange:(UITextField *)theTextField{
    NSUInteger subStr = 0;
    if (theTextField == self.phoneTextField) {
        subStr = 100;
    } else if (theTextField == self.pwdTextField) {
        if (self.loginType == LoginPWDType) {
            subStr = 16;
            //密码登录，  输入框 最多16位数
        }else{
            //验证码登录，  输入框 最多5位数
            subStr = 5;
        }
    }
    
    if (theTextField.text.length >subStr) {
        theTextField.text = [theTextField.text substringToIndex:subStr];
    }
    
    if (self.loginType == LoginPWDType) {
        if (self.phoneTextField.text.length >= 11 && self.pwdTextField.text.length >= 6 ) {
            [self setLoginButtonStatus:YES];
        } else {
            [self setLoginButtonStatus:NO];
        }
    } else {
        if (self.phoneTextField.text.length >= 11) {
            if (self.pwdTextField.text.length >= 5) {
                [self setLoginButtonStatus:YES];
            } else {
                [self setLoginButtonStatus:NO];
            }
        } else {
            self.countdownBtn.enabled = NO;
            [self setLoginButtonStatus:NO];
        }
    }
}

#pragma mark - setter

- (UIButton *)accountBtn{
    if (!_accountBtn) {
        _accountBtn = [[UIButton alloc] init];
        [_accountBtn addTarget:self action:@selector(clickAccountAction:) forControlEvents:UIControlEventTouchUpInside];
        _accountBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_accountBtn setTitle:@"账号登录" forState:UIControlStateNormal];
        [_accountBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        [_accountBtn setTitleColor:UIColorFromRGB(0x37C8D7) forState:UIControlStateSelected];
        _accountBtn.selected = YES;
    }
    return _accountBtn;
}

- (UIButton *)smsBtn{
    if (!_smsBtn) {
        _smsBtn = [[UIButton alloc] init];
        [_smsBtn addTarget:self action:@selector(clickSmsAction:) forControlEvents:UIControlEventTouchUpInside];
        _smsBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_smsBtn setTitle:@"快速登录" forState:UIControlStateNormal];
        [_smsBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        [_smsBtn setTitleColor:UIColorFromRGB(0x37C8D7) forState:UIControlStateSelected];
    }
    return _smsBtn;
}



- (UITextField *)phoneTextField {
    if (!_phoneTextField) {
        _phoneTextField = [[UITextField alloc] init];
        _phoneTextField.font = [UIFont systemFontOfSize:17];
        _phoneTextField.layer.borderWidth = 0;
        _phoneTextField.placeholder = @"请输入手机号码";
        _phoneTextField.textAlignment = NSTextAlignmentLeft;
        _phoneTextField.delegate = self;
        [_phoneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

        UIView *underline=[[UIView alloc] init];
        underline.backgroundColor=UIColorFromRGB(0xf5f5f5);
        [self.phoneTextField addSubview:underline];
        
        [underline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.phoneTextField.mas_bottom).offset(-1);
            make.left.mas_equalTo(self.phoneTextField.mas_left);
            make.height.mas_equalTo(1);
            make.right.mas_equalTo(self.phoneTextField.mas_right);
        }];
        

        
        
    }
    return _phoneTextField;
}

-(UITextField *)pwdTextField{
    if (!_pwdTextField) {
        _pwdTextField=[[UITextField alloc] init];
        _pwdTextField.font=[UIFont systemFontOfSize:17];
        _pwdTextField.layer.borderWidth=0;
        _pwdTextField.placeholder=@"请输入密码";
        _pwdTextField.textAlignment=NSTextAlignmentLeft;
        _pwdTextField.delegate = self;
        [_pwdTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

        UIView *underline2=[[UIView alloc] init];
        underline2.backgroundColor=UIColorFromRGB(0xf5f5f5);
        [self.pwdTextField addSubview:underline2];
        self.pwdBottomLine = underline2;
        [underline2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.pwdTextField.mas_bottom).offset(-1);
            make.left.mas_equalTo(self.pwdTextField.mas_left);
            make.height.mas_equalTo(1);
            make.right.mas_equalTo(self.pwdTextField.mas_right);
        }];
    }
    return _pwdTextField;
}

- (UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = UIColorFromRGB(0x333333);
    }
    return _lineView;
}

- (JGCountDownButton *)countdownBtn
{
    if (!_countdownBtn) {
        _countdownBtn = [JGCountDownButton countDownButtonWithTitle:@"获取验证码"
                                                     countDownTitle:@"秒后重新获取"
                                                         titleColor:UIColorFromRGB(0x37C8D7)
                                                countDownTitleColor:UIColorFromRGB(0x0099FF)];
        _countdownBtn.time = 60;
        _countdownBtn.titleLabel.font=[UIFont systemFontOfSize:17];
        _countdownBtn.titleLabel.textAlignment=NSTextAlignmentCenter;
      //  _countdownBtn.backgroundColor=UIColorFromRGB(0xf5f5f5);
 //       _countdownBtn.size = CGSizeMake(kAdaptedWidth(110), kAdaptedWidth(60));
        [_countdownBtn addTarget:self action:@selector(clickAuthBtnAction:)
                forControlEvents:UIControlEventTouchUpInside];
    }
    return _countdownBtn;
}

- (UIButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn addTarget:self action:@selector(clickLoginAction) forControlEvents:UIControlEventTouchUpInside];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        _loginBtn.titleLabel.font=[UIFont systemFontOfSize:16];
        _loginBtn.layer.cornerRadius = 4;
        _loginBtn.clipsToBounds = YES;
        _loginBtn.backgroundColor = UIColorFromRGB(0x37C8D7);
//        [_loginBtn setTitleColor:UIColorFromRGB(0x37C8D7) forState:UIControlStateNormal];
    }
    return _loginBtn;
}
@end
